//获取应用实例
const app = getApp();
const network_utils = require('../../../../http/NetworkUtils.js');
const constants = require('../../../../common/Constants.js');
const charts = require('../../../../common/view/mars/charts');
const util = require('../../../../utils/UmfUtils.js');

import PageRouter from '../../../../utils/PageRouter';

Page({
    data: {
        deviceW: 0,
        userId: '',
        totalAmountMonth: [],
        numMonth: [],
        merchantMonth: [],
    },

    onLoad: function (options) {
        let userId = options.userId;
        this.setData({
            userId: userId
        });

        wx.getSystemInfo({
            success: (res) => {
                this.setData({
                    deviceW: res.windowWidth,
                })
            }
        });
    },

    onReady: function () {
        let that = this;
        let params = {
            userId: this.data.userId,
            agentId: app.globalData.userData.agentId
        };

        network_utils.buildRequest(constants.Interfaces.FUNC_Merchant_JYGKT, params, {
            doHttpSucess: function (data) {
                that.setData({
                    dataISNull: constants.RetCodes.EMPTY_MERCHANT === data.retCode ? true : false
                });
                if (constants.RetCodes.SUCCESS === data.retCode) {
                    wx.stopPullDownRefresh();
                    that._initGraph(data);
                    return true;
                }
                return false;
            },
        }).send();
    },

    canvasClick: function () {
        PageRouter.wxNavigateTo(this, '../../../myMerchant/yesterdayTrad/yesterdayTrad');
    },


    //初始化图表
    _initGraph: function (data) {
        let graphCountArray = [data["storeSumAmounts"], data["storeSumNums"], data["storeSumAdds"]];
        let amount = data["storeSumAmount"] / 100;
        this.setData({
            busSumAmount: amount.toFixed(2),
            busSumNum: data["storeSumNum"],
            busSumAdd: data["storeSumAdd"]
        })
        for (var i = 0; i < graphCountArray.length; i++) {
            let graphDataArray = graphCountArray[i];
            let params = {}
            params.width = this.data.deviceW;
            params.height = 120;
            params.canvas_id = 'lineGraph' + i;
            params.ytitle = '';
            params.min = 0;
            params.yAxisSplit = 2;
            let xcateArray = [];
            let pointArray = [];
            for (let j = graphDataArray.length - 1; j >= 0; j--) {
                let dataArray = graphDataArray[j];
                xcateArray.push(util.getMonthWithDate(dataArray["date"]));
                if (i == 0) {
                    pointArray.push(parseFloat(dataArray["amount"]) / 100);
                } else {
                    pointArray.push(parseInt(dataArray["num"]));
                }

            }
            if (i == 0) {
                this.setData({
                    totalAmountMonth: xcateArray
                })

                params.data = [{
                    name: '月份',
                    data: pointArray,
                    format: function (val) {
                        return val.toFixed(2);
                    }
                }];
            } else {
                if (i == 1) {
                    this.setData({
                        numMonth: xcateArray
                    })
                } else {
                    this.setData({
                        merchantMonth: xcateArray
                    })
                }
                params.data = [{
                    name: '月份',
                    data: pointArray,
                    format: function (val) {
                        return val.toFixed(0);
                    }
                }];
            }
            params.xcate = xcateArray;
            charts.shapeLine(params);
        }
    },
});